package com.edu.zut.hb.software.server.controller;


import com.edu.zut.hb.software.server.service.IAdminService;
import com.edu.zut.hb.software.server.pojo.Admin;
import com.edu.zut.hb.software.server.pojo.RespBean;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zhoubin
 * @since 2021-08-10
 */
@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private IAdminService adminService;

    @ApiOperation(value = "获取所有管理员")
    @GetMapping("/")
    public List<Admin> getAllAdmins(){
        return adminService.list();
    }

    @ApiOperation(value = "添加管理员")
    @PostMapping("/")
    public RespBean addAdmin(@RequestBody Admin admin){
        if (adminService.save(admin)){
            return RespBean.success("添加成功！");
        }
        return RespBean.error("添加失败！");
    }
    @ApiOperation(value = "删除管理员")
    @DeleteMapping("/{id}")
    public RespBean deletAdmin(@PathVariable String id){
        if (adminService.removeById(id)){
            return RespBean.success("删除成功！");
        }
        return RespBean.error("删除失败！");
    }

    @ApiOperation(value = "更新管理员")
    @PutMapping("/")
    public RespBean updateAdmin(@RequestBody Admin admin){
        if (adminService.updateById(admin)){
            return RespBean.success("更新成功！");
        }
        return RespBean.error("更新失败！");
    }
    @ApiOperation(value = "获取单个管理员信息")
    @GetMapping("/{name}")
    public List<Admin> getOneAdmin(@PathVariable String name){
        return adminService.getOneAdmin(name);
    }

}
